/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.search;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;

import softstarter.functionality.RunnableItem;


public class SearchEngine {

	FileObjectComparator comparator;
	
	
	public SearchEngine() {
		comparator = new FileObjectComparator();
	}
	
	
	public List<RunnableItem> getTopHits(List<RunnableItem> runnableItems, String searchword, int numberOfHits) {
		List<RunnableItem> tophits = new ArrayList<RunnableItem>(numberOfHits);
		
		if(runnableItems.size() <= 0)
			return tophits;
		
		PriorityQueue<RunnableItem> orderedObjects = new PriorityQueue<RunnableItem>(runnableItems.size(), comparator);
		
		for(RunnableItem object : runnableItems) {
			object.setMatchingScore(countMatchingScore(object, searchword));
			orderedObjects.add(object);
		}
		
		for(int ii = 9; ii > 0; ii --) {
			if(!orderedObjects.isEmpty())
				tophits.add(orderedObjects.poll());
		}
		
		return tophits;
	}
	
	
	public int countMatchingScore(RunnableItem object, String searchword) {
		
		String fileName = object.getName().toLowerCase();
		searchword = searchword.toLowerCase();
		
		if(fileName.equalsIgnoreCase(searchword)) {
			return Integer.MIN_VALUE;
		} else if(fileName.startsWith(searchword)) {
			return -(searchword.length() * 40);
		} else if(fileName.indexOf(searchword) != -1) {
			return -(searchword.length() * 10);
		} 
		
		return 0;
	}
	
	
	public static void main(String[] args) {
		SearchEngine s = new SearchEngine();
		List<RunnableItem> files = new LinkedList<RunnableItem>();
		files.add(new RunnableItem("notepad.exe", "C:\\WINDOWS\\system32\\notepad.exe"));
		files.add(new RunnableItem("New text document.txt", "C:\\Documents and Settings\\viljakainenar\\Desktop\\New Text Document.txt"));
		files.add(new RunnableItem("kolkki.txt", "D:\\testifolder\\kolkki.txt"));
		
		System.out.println(s.countMatchingScore(new RunnableItem("notepad.exe", "c:\\notepad.exe"), "not"));
		System.out.println(s.countMatchingScore(new RunnableItem("New text document.txt", "c:\\New text document.txt"), "not"));

		List<RunnableItem> sorted = s.getTopHits(files, "n", 5);
		
		for(RunnableItem f : sorted) 
			System.out.println(f + " " + f.getMatchingScore());
	}
	
}
